<?php
//This script is used when clicked on a href from the tablbe in favourites.php.
//It uses the url as an ID to find correct element for counter and increase by 1.
//Saxon Dakin, 7392176 

//Gets url from favourites form via $_GET.
//$data is used later to write to file.
$url = $_GET['id'];
$data = "";

//Reads file into single array then splits into a multidimensional array.
//$bigarray into $smallarray.
$bigarray = file("favourites.txt");
 
$fh = fopen("favourites.txt","w");
fclose($fh); 
$fh1 = fopen("favourites.txt","a");   
$bool = true;

foreach ($bigarray as $key => $value) 
{
   $smallarray=explode("|", $value);
   if (!empty($smallarray[0]))
   {
   $data = "";
   
           //Just to check if this is the first line, this fixs any trouble caused.
           //by it adding new lines.
           if($bool)
           {
            $bool =false;
           }
           else
           {
            $data .= "\n";
           }
           
          //Loop to find correct url to increase the 4th element by 1.
          if($smallarray[0] == $url)
          {
            
            $data .= $smallarray[0] . "|" . $smallarray[1] . "|" . $smallarray[2] . "|" . (int)($smallarray[3]+1); 
            fwrite($fh1, $data);
          }
          else
          {
            $data .= $smallarray[0] . "|" . $smallarray[1] . "|" . $smallarray[2] . "|" . (int)$smallarray[3]; 
            fwrite($fh1, $data);
          }
   } 
} 
//A refresh that loads the selected page.
echo "<meta http-equiv='Refresh' content='1;URL=" . $url . "' />";
 
?>
